package com.xoa.plugin.ct;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xoa.model.nccloud.NccPsninfo;
import com.xoa.model.nccloud.NccWaGradeV;
import com.xoa.model.nccloud.NccWaPrmlvV;
import com.xoa.service.nccloud.INCCPsndocService;
import com.xoa.service.nccloud.INCCViewService;

import java.util.List;
import java.util.Map;


/**
 * 城投---调薪表单
 */
public class CTAdjustWagesTrigger {


    /**
     * 表单数据拼接json调资
     *
     * @param formdata
     * @param runId
     * @return
     * @throws Exception
     */

    public static String onversioncDto284(String formdata, String runId, INCCViewService viewService, INCCPsndocService psndocService) throws Exception {

        //将表单数据json数组解析List<Map<String, Object>>
        List<Map<String, Object>> data = JSONArray.parseObject(formdata, List.class);
        //存储人员数组对象
        JSONArray arrayPersObject = new JSONArray();
        for (Map<String, Object> datum : data) {
            switch (datum.get("key").toString()) {

                case "DATA_16"://人员信息
                    String value = datum.get("value").toString();

                    value = value.substring(0, value.indexOf("|"));
                    String[] values = value.split("\r\n");
                    for (int i = 0; i < values.length; i++) {
                        String val = values[i] + "``1`";
                        String[] strs = val.split("`");

                        JSONObject jsonPersObject = new JSONObject();
                        jsonPersObject.put("data1", strs[0]);//姓名
                        jsonPersObject.put("data2", strs[1]);//所在部门
                        jsonPersObject.put("data3", strs[2]);//岗位
                        jsonPersObject.put("data4", strs[3]);//参加工作时间
                        jsonPersObject.put("data5", strs[4]);//入职时间
                        jsonPersObject.put("data6", strs[5]);//职级

                        jsonPersObject.put("data7", strs[6]);//薪级

                        jsonPersObject.put("data8", strs[7]);//基础保障
                        jsonPersObject.put("data9", strs[8]);//工龄,司龄工资
                        jsonPersObject.put("data10", strs[9]);//职称工资
                        jsonPersObject.put("data11", strs[10]);//小计

                        jsonPersObject.put("data12", strs[11]);//月岗位工资A2
                        jsonPersObject.put("data13", strs[12]);//月绩效工资A3
                        jsonPersObject.put("data14", strs[13]);//学历补贴A4
                        jsonPersObject.put("data15", strs[14]);//工资月薪税前收入 A5
                        jsonPersObject.put("data16", strs[15]);//基础保障
                        jsonPersObject.put("data17", strs[16]);//工龄,司龄工资
                        jsonPersObject.put("data18", strs[17]);//职称工资
                        jsonPersObject.put("data19", strs[18]);//小计

                        jsonPersObject.put("data20", strs[19]);//岗位工资B2
                        jsonPersObject.put("data21", strs[20]);//绩效工资B3
                        jsonPersObject.put("data22", strs[21]);//学历补贴B4
                        jsonPersObject.put("data23", strs[22]);//工资月薪税前收入B5
                        jsonPersObject.put("data24", strs[23]);//工号
                        jsonPersObject.put("data25", strs[24]);//薪资起始日期

                        jsonPersObject.put("data26", strs[25]);//月岗位工资A2-id
                        jsonPersObject.put("data27", strs[26]);//月绩效工资A3-id


                        if (!jsonPersObject.getString("data26").equals("") && !jsonPersObject.getString("data27").equals("")) {
                            JSONObject jsonObject2 = (JSONObject) jsonPersObject.clone();

                            jsonPersObject.put("data21", "");//绩效工资B3
                            jsonPersObject.put("data27", "");//月绩效工资A3-id
                            arrayPersObject.add(jsonPersObject);

                            jsonObject2.put("data20", "");//岗位工资B2
                            jsonObject2.put("data26", "");//月岗位工资A2-id
                            arrayPersObject.add(jsonObject2);

                        } else {
                            arrayPersObject.add(jsonPersObject);
                        }
                    }
                    break;

            }
        }


        JSONArray arrayParamObject = new JSONArray();
        for (int a = 0; a < arrayPersObject.size(); a++) {
            JSONObject jsonParamObject = new JSONObject();

            //查询人员信息并添加
            NccPsninfo nccPsninfo = new NccPsninfo();
            nccPsninfo.setCode(arrayPersObject.getJSONObject(a).getString("data24"));
            List<NccPsninfo> psninfoList = psndocService.selectPsn(nccPsninfo);
            if (null == psninfoList || psninfoList.size() == 0) {
                throw new RuntimeException("人员" + arrayPersObject.getJSONObject(a).get("data1") + "查询不到！");
            } else {
                jsonParamObject.put("pk_org", psninfoList.get(0).getPkOrgCode()); //组织
                jsonParamObject.put("pk_psndoc", psninfoList.get(0).getCode()); //人员编码
            }


            NccWaPrmlvV nccWaPrmlvV = new NccWaPrmlvV();

            if (!arrayPersObject.getJSONObject(a).getString("data26").equals("")) {
                nccWaPrmlvV.setId(arrayPersObject.getJSONObject(a).getLong("data26"));
            } else if (!arrayPersObject.getJSONObject(a).getString("data27").equals("")) {
                nccWaPrmlvV.setId(arrayPersObject.getJSONObject(a).getLong("data27"));
            }
            List<NccWaPrmlvV> nccWaPrmlvVS = viewService.selecWaprmlv(nccWaPrmlvV);
            if (null == nccWaPrmlvVS || nccWaPrmlvVS.size() == 0) {
                throw new RuntimeException("查询薪资级别无！");
            } else {
                jsonParamObject.put("pk_wa_crt_showname", nccWaPrmlvVS.get(0).getPrmlvname()); //薪资标准名称
                jsonParamObject.put("pk_wa_prmlv", nccWaPrmlvVS.get(0).getPkWaPrmlv()); //薪资标准级别主键
                jsonParamObject.put("criterionvalue", nccWaPrmlvVS.get(0).getCriterionvalue()); //标志金额
                jsonParamObject.put("nmoney", nccWaPrmlvVS.get(0).getCriterionvalue()); //金额
                jsonParamObject.put("pk_wa_crt", nccWaPrmlvVS.get(0).getPkWaCrt()); //薪资标准主键
                if (null != nccWaPrmlvVS.get(0).getPkWaSeclv() && !nccWaPrmlvVS.get(0).getPkWaSeclv().equals("")) {
                    jsonParamObject.put("pk_wa_seclv", nccWaPrmlvVS.get(0).getPkWaSeclv());
                }
            }

            NccWaGradeV nccWaGradeV = new NccWaGradeV();
            nccWaGradeV.setPkWaGrd(nccWaPrmlvVS.get(0).getPkWaGrd());
            List<NccWaGradeV> nccWaGradeVS = viewService.selectWaGrad(nccWaGradeV);
            if (null == nccWaGradeVS || nccWaGradeVS.size() == 0) {
                throw new RuntimeException("查询薪资标准类别无！");
            } else {
                jsonParamObject.put("pk_wa_grd", nccWaGradeVS.get(0).getPkWaGrd()); //薪资标准类别主键
                jsonParamObject.put("pk_wa_item", nccWaGradeVS.get(0).getPkWaItem()); //薪资项目主键
            }


            jsonParamObject.put("begindate", arrayPersObject.getJSONObject(a).getString("data25")); //薪资起始日期
            jsonParamObject.put("enddate", ""); //薪资结束日期
            jsonParamObject.put("changedate", ""); //薪资调整日期


            jsonParamObject.put("pk_group", "0001A210000000000GQS"); //集团
            jsonParamObject.put("waflag", "Y"); //发放标志
            jsonParamObject.put("lastflag", "Y"); //最近记录标志
            jsonParamObject.put("negotiation_wage", "N"); //是否是谈判工资
            jsonParamObject.put("partflag", "Y"); //兼职标志
            jsonParamObject.put("vbasefile", "无"); //依据文件名称


            arrayParamObject.add(jsonParamObject);


        }


        return arrayParamObject.toJSONString();
    }
}
